<?php

namespace sms_cs;

/**
 * SMSCSClient
 *
 * @author DevPHP
 * @version 2018-4-12
 */
class SMSCSClient {

    /**
     * 
     * @param type $mobile
     * @param string $ip
     * @return type
     * @version 2018-4-12
     */
    static function check_filter($mobile, $ip) {
        $mobileBlackList = [];
        $ipBlackList = [];
        $mobileWhiteList = [];
        $ipWhiteList = [];
        $ipMax = 20;
        $phoneMax = 10;
        $ip = '';
        $return = ['status' => 0, 'info' => ''];
        if ($mobileBlackList && in_array($mobile, $mobileBlackList)) {
            $return['message'] = '手机号已被系统列为黑名单!';
            return $return;
        }
        if ($ipBlackList && in_array($ip, $ipBlackList)) {
            $return['message'] = 'IP已被系统列为黑名单!';
            return $return;
        }
        if ($phoneMax > 0 && (!$mobileWhiteList || !in_array($mobile, $mobileWhiteList))) {
            $count = db('sms_login_code_log')->where(['mobile' => $mobile])->count();
            if ($count > $phoneMax) {
                $return['message'] = '该手机号超过每天最大短信发送次数[' . $phoneMax . ']!';
                return $return;
            }
        }
        if ($ipMax > 0 && (!$ipWhiteList || !in_array($ip, $ipWhiteList))) {
            $count = db('sms_login_code_log')->where(['ip' => $ip])->count();
            if ($count > $ipMax) {
                $return['message'] = '该手机号超过每天最大短信发送次数[' . $phoneMax . ']!';
                return $return;
            }
        }
    }
    public function sendByAliyunSms(){
        
    }
    public function sendByQcloudSms(){
        
    }

    /**
     * 
     * @param type $mobile
     * @return status,info,id,channel_name
     */
    public function sendByYunpian($mobile,$apikey) {
        $apikey=config('sdkaccount.yunpian_apikey');
        $api=new driver\YunpianSmsApi($apikey);
        return $api->single_send($mobile);
    }

    public function sendByJuheSms($mobile,$key,$tpl_id,$tpl_value) {

        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL

        $smsConf = array(
            'key' => 'e269aa026f0314619f7f719ca04781fe', //您申请的APPKEY
            'mobile' => is_array($mobile_number) ? implode(',', $mobile_number) : $mobile_number, //接受短信的用户手机号码
            'tpl_id' => '49976', //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => $content //您设置的模板变量，根据实际情况修改 (#code#=1234&#company#=聚合数据)
        );

        $content = $this->juhecurl($sendUrl, $smsConf, 1); //请求发送短信

        if ($content) {
            $result = json_decode($content, true);
            $error_code = $result['error_code'];
            if ($error_code == 0) {
                //状态为0，说明短信发送成功
                $data['status'] = 1;
                $data['msg'] = "短信发送成功,短信ID：" . $result['result']['sid'];
            } else {
                //状态非0，说明失败
                $data['status'] = 0;
                $data['msg'] = "短信发送失败(" . $error_code . ")：" . $result['reason'];
            }
        } else {
            //返回内容异常，以下可根据业务逻辑自行修改
            $data['status'] = 0;
            $data['msg'] = "请求发送短信失败";
        }
        return $data;
    }

}
